package com.idsmanager.zdh.service.business.api;

import com.idsmanager.commons.web.WebUtils;
import com.idsmanager.commons.web.context.BeanProvider;
import com.idsmanager.oauth.domain.security.User;
import com.idsmanager.oauth.domain.security.UserRepository;
import com.idsmanager.zdh.infrastructure.APIError;
import com.idsmanager.zdh.service.dto.api.UserApiResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RestFindUserByUsernameLoader {

    private static final Logger LOG = LoggerFactory.getLogger(RestFindUserByUsernameLoader.class);

    private transient UserRepository userRepository = BeanProvider.getBean(UserRepository.class);

    public UserApiResult load(String username){
        User user = userRepository.findByUsername(username);
        if (user == null) {
            LOG.debug("{}|not found user by username:{}", WebUtils.getIp(), username);
            return new UserApiResult(APIError.PARAM_ERROR, "未知的username:" + username);
        }
        return new UserApiResult(user);
    }
}
